<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *  Class Dynamic Menu
 *
 *  This class is used to build dynamic JQuery Menu
 */

class Dynamic_Menu
{	
	var $status 		= TRUE;
	var $root_image		= "&darr;";
	var $parent_class 	= "class=\"childs\" style=\"display:none;\"";
	
	/**
	* Constructor
	*
	* @access    public
	* @param    array    initialization parameters
	*/
	function Dynamic_Menu($params = array())
	{				
		if (count($params) > 0)
		{
			$this->initialize($params);        
		}
		log_message('debug', "Dynamic Menu Class Initialized");
	}
	
	// --------------------------------------------------------------------
    
    /**
     * Initialize Preferences
     *
     * @access    public
     * @param     array    initialization parameters
     * @return    void
     */
    function initialize($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = $val;
                }
            }        
        }
    }
	
	// --------------------------------------------------------------------
    
    /**
     * Generate Menu
     *
     * @access    public
     * @return    string
     */
	function generate_menu()
	{
		if($this->status == TRUE)
		{
			$str_menu = "";
			$CI =& get_instance();
			$query = "SELECT id,menu_title,menu_url,parent FROM ".TBL_MENU." 
						WHERE status = 1 AND parent = 0";
			$result = $CI->db->query($query);
			if($result->num_rows > 0)
			{
				foreach($result->result_array() as $root)
				{
					$str_menu .= "<li>";
					if($this->has_childs($root['id']))
					{
						$str_menu .= $this->generate_item_root($root,$this->root_image);
						$str_menu .= $this->add_parent($root['id']);				
					}
					else
					{
						$str_menu .= $this->generate_item_root($root);
					}
					$str_menu .= "</li>";
				
				}	
			}
			echo $str_menu;		
		}
	}
	
	// --------------------------------------------------------------------
	    
    /**
     * Add Parent
     *
     * @access    public
	 * @param     integer  
     * @return    string
     */
	function add_parent($parent=0)
	{
		$str_menu = "";
		$CI =& get_instance();
		// Process DB request
		$query = "SELECT id,menu_title,menu_url,parent FROM ".TBL_MENU." 
					WHERE status = 1 AND parent = ".$parent;
		$result = $CI->db->query($query);
		if($result->num_rows > 0)
		{	
			$str_menu .= "<ul ".$this->parent_class.">";
			foreach($result->result_array() as $key => $childs)
			{
				if(($result->num_rows - 1) == $key)
					$last_element = "last";
				else
					$last_element = "cl";	
					
				$str_menu .= "<li>";
				if($this->has_childs($childs['id']))
				{
					$str_menu .= $this->generate_item_child($childs,$last_element);
					$str_menu .= $this->add_parent($childs['id']);
				}
				else
				{
					$str_menu .= $this->generate_item_child($childs,$last_element);
				}
				$str_menu .= "</li>";	
			}
			$str_menu .= "</ul>";
		}
		
		return $str_menu;
	}
	
	// --------------------------------------------------------------------	
	
	/***
     * Has Childs
     *
     * @access    public
	 * @param     integer  
     * @return    integer
     */
	function has_childs($parent=0)
	{
		$CI =& get_instance();
		
		$query 			= "SELECT id,menu_title,menu_url,parent FROM ".TBL_MENU." 
							WHERE status = 1 AND parent =".$parent;
		$result_childs 	= $CI->db->query($query);
		return $result_childs->num_rows;
	}	
		
	// --------------------------------------------------------------------
    
    /**
     * Generate Item Root
     *
     * @access    public
     * @param     array
	 * @param     string  
     * @return    string
     */
	function generate_item_root($itmes_array=array(),$has_childs='')
	{
		$str = "";
		if(isset($itmes_array) && !empty($itmes_array))
		{
			$str .= "<a href=\"". site_url($itmes_array["menu_url"]) ."\" title=\"". $itmes_array["menu_title"] ."\" >";		
			if(!empty($has_childs))
			{
				$str .= "<span style=\"padding: 0 3px 3px 0\">". $has_childs ."</span>";
			}
			$str .= $itmes_array["menu_title"] ."</a>";	
				
		}
		
		return $str ;
	}
	
	// --------------------------------------------------------------------
    
    /**
     * Generate Item Child
     *
     * @access    public
     * @param     array
	 * @param     string  
     * @return    string
     */
	function generate_item_child($itmes_array=array(),$class="cl")
	{
		if(isset($itmes_array) && !empty($itmes_array))
		{
			return "<a href=\"". site_url($itmes_array["menu_url"]) ."\"  title=\"". $itmes_array["menu_title"] ."\"><span class=\"".$class."\">". $itmes_array["menu_title"] ."</span></a>";
		}
	}
	
}
?>